﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using DataEntity;
using JScripter;
using BusinessLogic.Controller;

public partial class Admin_AjaxPages_PropertyTypeAdd : AjaxPage
{
    private int ID
    {
        get
        {
            if (Request.Params["ptid"] != null || AjaxState.ContainsKey("ptid"))
            {
                if (!AjaxState.ContainsKey("ptid"))
                    AjaxState["ptid"] = Request.Params["ptid"];
                return Convert.ToInt32(AjaxState["ptid"]);
            }
            else
            {
                return 0;
            }
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        if (ID > 0)
        {
            BindData();
        }
        base.OnLoad(e);
    }
    private void AddData()
    {
        try
        {
            PropertyType objPropertyType = new PropertyType();

            //objPropertyType.PropertyTypeID = Guid.NewGuid();

            objPropertyType.Name = HtmlHelper.ControlValue(txtName.ClientID);

            objPropertyType.IsResidential = Convert.ToBoolean(HtmlHelper.CheckBoxControlValue(chkIsResidential.ClientID));

            objPropertyType.IsCommercial = Convert.ToBoolean(HtmlHelper.CheckBoxControlValue(chkIsCommercial.ClientID));

            objPropertyType.ModifiedDate = DateTime.Now;

            new PropertyTypeController().Add(objPropertyType);
            Response.Redirect("~/Admin/AjaxPages/PropertyTypeList.aspx");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = "<div class='error'>" + ex.Message + "</div>";
        }
    }

    private void EditData()
    {

        PropertyType objPropertyType = new PropertyType();

        objPropertyType.PropertyTypeID = ID;

        objPropertyType.Name = HtmlHelper.ControlValue(txtName.ClientID);

        objPropertyType.IsResidential = Convert.ToBoolean(HtmlHelper.CheckBoxControlValue(chkIsResidential.ClientID));

        objPropertyType.IsCommercial = Convert.ToBoolean(HtmlHelper.CheckBoxControlValue(chkIsCommercial.ClientID));

        objPropertyType.ModifiedDate = DateTime.Now;

        new PropertyTypeController().UpdateByPropertyTypeID(objPropertyType);
        Response.Redirect("~/Admin/AjaxPages/PropertyTypeList.aspx");
    }

    private void BindData()
    {
        var dataBunch = new PropertyTypeController().GetbyPropertyTypeID(ID);
        if (dataBunch.Count > 0)
        {
            var data = dataBunch[0];

           
            txtName.Text = data.Name;

            chkIsResidential.Checked = data.IsResidential;

            chkIsCommercial.Checked = data.IsCommercial;

           

        }
    }

    protected void AddAjaxClick(object sender, AjaxControl.AjaxEventArg e)
    {
        AddData();

    }
    protected void UpdateAjaxClick(object sender, AjaxControl.AjaxEventArg e)
    {
        EditData();
    }
    public void PreventData()
    {
        txtName.Text = HtmlHelper.ControlValue(txtName.ClientID);
        chkIsResidential.Checked = Convert.ToBoolean(HtmlHelper.ControlValue(chkIsResidential.ClientID));
        chkIsCommercial.Checked = Convert.ToBoolean(HtmlHelper.ControlValue(chkIsCommercial.ClientID));
        

    }

}